define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shopro/tokens/trade/index' + location.search,
                    add_url: 'shopro/tokens/trade/add',
                    edit_url: 'shopro/tokens/trade/edit',
                    del_url: 'shopro/tokens/trade/del',
                    multi_url: 'shopro/tokens/trade/multi',
                    import_url: 'shopro/tokens/trade/import',
                    table: 'shopro_tokens_trade',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'order_num', title: __('Order_num'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'sale.username', title: '卖方用户名', operate: 'LIKE'},
                        {field: 'sale.nickname', title: '卖方昵称', operate: 'LIKE'},
                        {field: 'buy.username', title: '买方用户名', operate: 'LIKE'},
                        {field: 'buy.nickname', title: '买方昵称', operate: 'LIKE'},
                        {field: 'account', title: __('Account'), operate:'BETWEEN'},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'total_price', title: __('Total_price'), operate:'BETWEEN'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'wallet_type', title: "兑换方向", searchList: {"commission":"福利券->通证","gold_bean":"金豆->通证"}, formatter: Table.api.formatter.status},
                        {field: 'pay_time', title: __('Pay_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons:[{
                                name: 'pass',
                                text: '取消订单',
                                title: '确认取消订单',
                                classname: 'btn btn-success btn-ajax',
                                icon: 'fa fa-check',
                                confirm: '确认取消订单吗?',
                                url:'shopro/tokens/trade/close_order?ids={row.id}',
                                visible:function (row){
                                    if(row.status===0 && Config.closeOrderAuth){
                                        return true
                                    }else{
                                        return false
                                    }
                                },
                                success: function (data, ret) {
                                    $(".btn-refresh").trigger("click");
                                },
                                error: function (data, ret) {
                                    $(".btn-refresh").trigger("click");
                                }
                            }]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
